package
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;

	import baidu.map.basetype.LngLat;
	import baidu.map.basetype.Size;
	import baidu.map.control.base.Copyright;
	import baidu.map.control.base.Navigator;
	import baidu.map.control.base.Ruler;
	import baidu.map.control.base.Scaler;
	import baidu.map.core.Map;
	import baidu.map.event.MapEvent;
	import baidu.map.event.MarkerEvent;
	import baidu.map.layer.Layer;
	import baidu.map.layer.RasterLayer;
	import baidu.map.overlay.InfoWindow;
	import baidu.map.overlay.Marker;
	import baidu.map.projection.MercatorProjection;
	import baidu.map.projection.Projection;

	import control.MyControl;

	import util.LabelButton;
	import util.ScalerSprite;

	/*
	* Demo
	* 本例使用Flash API展示一个地图。
	*/
	[SWF(width="960", height="560")]
	public class BaiduMap extends Sprite
	{
		[Embed(source="1.jpg")]
		private var MyDefaultCursor:Class;

		private var _map:Map;

		private var _lastLng:LngLat;

		private var _txt:TextField;

		private var _marker:Marker; /**玩家的logo*/

		private var txtAddress:TextField;

		private var txtCityName:TextField;





		public function BaiduMap()
		{

			addEventListener(Event.ADDED_TO_STAGE, onStageReady);

			_txt=new TextField();
			_txt.width=500;
			_txt.height=50;
			_txt.x=_txt.y=10;
			_txt.text="请点击地图任意位置";
			_txt.defaultTextFormat=new TextFormat("Verdana");
			this.addChild(_txt);
		}

		protected function onStageReady(evt:Event):void
		{
			stage.scaleMode=StageScaleMode.NO_SCALE;
			stage.align=StageAlign.TOP_LEFT;
			init();

			/**初始化创建覆盖物**/
			addOverLayer();
		}

		/**初始化*/
		private function init():void
		{
			// 舞台设置 
			removeEventListener(Event.ADDED_TO_STAGE, onStageReady);

			//			ExternalInterface.addCallback("LocalCityBackFun", LocalCityBackFun);
			//			ExternalInterface.addCallback("onGotoCityBack", onGotoCityBack);
			//			// 创建一个大小为600*400的Map对象
			//			ExternalInterface.call("GetLocalCity");


			initMap(new LngLat(113.2759952545166, 23.117055306224895), 12);
		}

		public function LocalCityBackFun(lng:Number, lat:Number, level:int, cityname:String):void
		{
			initMap(new LngLat(lng, lat), level);

			var txt:TextField=new TextField();
			txt.width=100;
			txt.height=20;

			addChild(txt);
			txt.text=cityname;


			txtAddress=new TextField();
			txtAddress.type=TextFieldType.INPUT;

			txtAddress.background=true;
			txtAddress.backgroundColor=0xff0000;
			txtAddress.x=100;
			txtAddress.width=100;
			txtAddress.height=25;
			addChild(txtAddress);

			txtCityName=new TextField();
			txtCityName.type=TextFieldType.INPUT;
			txtCityName.x=250
			txtCityName.width=100;
			txtCityName.height=25
			txtCityName.background=true;
			txtCityName.backgroundColor=0xff0000;
			addChild(txtCityName);

			var sh:Sprite=new Sprite();

			sh.graphics.beginFill(0x000000);
			sh.graphics.drawRect(0, 0, 30, 20);
			sh.graphics.endFill();
			this.addChild(sh);

			sh.x=360;
			sh.addEventListener(MouseEvent.CLICK, onClick);

			sh.mouseEnabled=true;
			sh.buttonMode=true;
			sh.useHandCursor=true;

		}

		protected function onGotoCityBack(lng:Number, lat:Number):void
		{
			_map.panTo(new LngLat(lng, lat));
		}

		protected function onClick(e:Event):void
		{
			ExternalInterface.call("GotoCity", txtAddress.text, txtCityName.text);
		}



		private function initMap(center:LngLat, level:int):void
		{

			_map=new Map(new Size(900, 500));
			_map.y=50;
			addChild(_map);
			// 初始化Map的中心点和显示级别
			_map.centerAndZoom(center, level);

			// 添加底图
			var layer:Layer=new RasterLayer("BaiduMap", _map);
			_map.addLayer(layer);

			
			_map.addEventListener(MapEvent.ZOOM_END, zoomEnd);
		}

		private function zoomEnd(e:MapEvent):void
		{

			var info:InfoWindow;
			if (_map.zoom >= 15)
			{
				_map.zoom=15;
				info=new InfoWindow("不能再近了！", null, "提醒");
				_map.openInfoWindow(info);
				return;
			}
			if (_map.zoom <= 6)
			{
				_map.zoom=6;
				info=new InfoWindow("不能再远了！", null, "提醒");
				_map.openInfoWindow(info);
				return;
			}
			_map.closeInfoWindow();

		}

		private function addOverLayer():void
		{
			removeByClass(_map, Copyright);

			var nav:Navigator=new Navigator(_map);
			_map.addControl(nav);

			var scaler:Scaler=new Scaler(_map);
			_map.addControl(scaler);
			scaler.addEventListener(MapEvent.DRAG_END, onTest);



			var ms:ScalerSprite=new ScalerSprite();
			addChild(ms);

			_map.addEventListener(MapEvent.CLICK, getClickLng);


			var bitmapdata:BitmapData=new zui();
			var bitmap:Bitmap=new Bitmap(bitmapdata);
			_marker=new Marker(bitmap);
			_marker.position=new LngLat(113.2759952545166, 23.117055306224895);
			_map.addOverlay(_marker);
			_marker.enableDragging=true;
			_marker.addEventListener(MarkerEvent.DRAG_END, dragEnd);

			var _ruler:Ruler=new Ruler(_map);
			_map.addControl(_ruler);
			_ruler.x=10;

			var zoomOutBtn:LabelButton=new LabelButton("北京");
			addChild(zoomOutBtn);

			zoomOutBtn.x=500;
			zoomOutBtn.addEventListener(MouseEvent.CLICK, moveTo);

			var labJiuba:MyControl=new MyControl(_map);
			_map.addControl(labJiuba);

		}

		private function onTest(e:MapEvent):void
		{
			trace("123");

		}

		private function moveTo(e):void
		{
			go(116.46, 39.92); //北京
		}

		/**
		 * @定位地图
		 * @longitude:Number,经度
		 * @latitude:Number纬度
		 */
		private function go(longitude:Number, latitude:Number):void
		{
			var lnglat:LngLat=new LngLat(longitude, latitude);
			_map.panTo(lnglat);
		}

		private function dragEnd(e:Event):void
		{


		}

		private var f:uint;

		/**
		 * @*两点之间在现实生活中的大概距离*
		 * */
		protected function getClickLng(event:MapEvent):void
		{
			var proj:Projection=new MercatorProjection();
			var ln:LngLat=_map.pixelToLngLat(new Point(_map.mouseX, _map.mouseY));

			var c:Number=0;
			if (_lastLng != null)
			{
				var e:Number;
				e=Math.min(Math.abs(_lastLng.lat), Math.abs(ln.lat));
				var a:Number=111 * Math.abs(ln.lng - _lastLng.lng) * Math.cos(_lastLng.lat * Math.PI / 180);
				var b:Number=111 * Math.abs(ln.lat - _lastLng.lat);
				c=Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
			}
			_lastLng=ln;
			_txt.text="鼠标点击位置经度：" + ln.lng + " 纬度：" + ln.lat;
			_txt.appendText("\n与上次点击位置的距离：" + c.toFixed(5) + " km");


			var marker:Marker=new Marker();
			marker.position=new LngLat(ln.lng, ln.lat);
			_map.addOverlay(marker);

			var txt:TextField=new TextField();
			f++;
			txt.text=String(f);
			txt.x=-5;
			txt.y=-20;
			marker.addChild(txt);

			onmapclick();
		}

		/**
		 *
		 * *@获取鼠标点击地点的坐标
		 */
		private function onmapclick():void
		{

			trace(_map.pixelToLngLat(new Point(_map.mouseX, _map.mouseY)));

		}

		/****
		 * @去掉logo
		 * **/
		private function removeByClass(dis:DisplayObjectContainer, c:Class):Boolean
		{

			if (dis == null)
			{
				return false;
			}
			if (dis is c)
			{
				dis.parent.removeChild(dis);
				return true;
			}
			var count:int=dis.numChildren;
			for (var i:int=0; i < count; i++)
			{
				if (removeByClass(dis.getChildAt(i) as DisplayObjectContainer, c))
					return true;
			}
			return false;
		}
	}
}
